from PySide6.QtWidgets import QWidget,QTableWidgetItem
from sqlalchemy.orm import Session
from settings.publicvar import engine
from views.book.author.author import Ui_author
from views.book.mode.book import Author

class AuthorView(QWidget,Ui_author):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.loadTableData()

     # 表格结构
    def SetTableStructure(self, data):
        self.authortable.setRowCount(len(data))
        for i, item in enumerate(data):
            idx = QTableWidgetItem(str(item.id))
            self.authortable.setItem(i, 0, idx)
            name = QTableWidgetItem(item.name)
            self.authortable.setItem(i,1,name)

    # 加载表格数据
    def loadTableData(self):
        with Session(engine) as session, session.begin():
            authors = session.query(Author).all()
            self.SetTableStructure(authors)
